#include "head_c.h"

int net_fd;
user_info info;

void func(int num) {
    info.command_num = 10;
    sendInfoStruct(net_fd, &info);
    //send(net_fd, &num, sizeof(int), MSG_NOSIGNAL);
    exit(1);
}

int main(int argc,char*argv[])
{


    signal(2, func);
    char ip[20];                                                                                                                  
    char port[10];
    memset(ip, 0, sizeof(ip));
    memset(port, 0, sizeof(port));
    getparameter("ip", ip);
    getparameter("port", port);

    //初始化火车（文件长度、文件信息）
    train_t train;
    bzero(&train, 0);
    //虚假地址
    //user_info info;
    memset(&info, 0, sizeof(info));
    char *str = "/";
    strcpy(info.path, str);
    info.path_tail = strlen(str);

    net_fd = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    addr.sin_addr.s_addr = inet_addr(ip);
    int ret_con = connect(net_fd, (struct sockaddr *)&addr, sizeof(addr));
    ERROR_CHECK(ret_con, -1, "connect");

    int epoll_fd = epoll_create(1);
    addEpoll(epoll_fd, net_fd);
    addEpoll(epoll_fd, STDIN_FILENO);

    //是否是登录
    bool flag = true;
    
    while(1) {
        //printf("%s:", pa.path);
        //fflush(stdout);

        if(flag) {
            printf("(a)登录  (b)注册\n");
        }else {
            printf("%s:", info.path);
        }
        fflush(stdout);
        fflush(stdin);

        struct epoll_event events[2];
        int epoll_num = epoll_wait(epoll_fd, events, 2, -1);

        //触发监听
        for(int i = 0; i < epoll_num; i++) {

            //char* str = (char*)malloc(1024 * sizeof(char));
            //memset(str, 0, 1024);

            //监听到标准输入
            if(events[i].data.fd == STDIN_FILENO) {
                info.command_num =-1;
                //printf("stdin");
                //char command_str[1024];
                memset(info.command_str_goto, 0, sizeof(info.command_str_goto));

                if(flag == true) {
                    //flag = false;
                    info.command_num = 0;
                }

                char *path = (char *)calloc(0, 1024);
                //char *temp_path = path;
                sprintf(path, "%s", "hello");
                free(path);
                
                if(info.command_num != 0) {
                    //获得指令
                    read(STDIN_FILENO, info.command_str_goto, sizeof(info.command_str_goto));
                    //进行指令切割 
                    path = splitCommand(info.command_str_goto, &info.command_num);
                }
                //memset(info.command_str_goto, 0, sizeof(info.command_str_goto));
                sprintf(info.command_str_goto, "%s", path);
                //free(temp_path);

                //printf("command_num=%d,command_str=%s\n", info.command_num, info.command_str_goto);
                //printf("info.command_str_goto=%s\n", info.command_str_goto);
                if(info.command_num == -1){
                    continue;
                }

                //发送指令
                //sendInfoStruct(net_fd, &info);
                
                //int flag_cd = 0;
                char *temp_str = NULL;
                char temp_ch;
                int temp_int;
                
                switch(info.command_num) {
                case 0:
                    //login
                    temp_str = (char *)calloc(0, 1024);
                    //printf("enter username:\n");
                    //scanf("%c", &temp_ch);
                    //printf("%s\n", _user);
                    //清缓冲区的回车
                    scanf("%c", &temp_ch);
                    getchar();
                    //while((temp_ch = getchar()) != '\n'){}
                    if(temp_ch != 'a' && temp_ch != 'b') {
                        flag = true;
                        break;
                    }
                    
                    printf("enter username:\n");
                    scanf("%s", info.username);
                    getchar();

                    printf("enter password:\n");
                    scanf("%s", temp_str);
                    getchar();

                    if(temp_ch == 'a') {
                        temp_int = userLoginClient(net_fd, &info, temp_str);
                        if(temp_int == 0 || temp_int == -1) {
                            //登录失败返回false
                            printf("用户名或密码错误\n");
                            flag = true;
                        }
                        else {
                            flag = false;
                            printf("登录成功\n");
                        }
                    }else {
                        temp_int = userRegisterClient(net_fd, &info, temp_str);
                        //temp_ch = b
                        if(temp_int == -1) {
                            printf("用户名已注册\n");
                            flag = true;
                        }
                        else {
                            flag = true;
                            printf("注册成功\n");
                        }
                    }
                    free(temp_str);
                    temp_str = NULL;

                    break;
                case 1:
                    //cd
                    sendInfoStruct(net_fd, &info);
                    recvInfoStruct(net_fd, &info);
                    if(info.flag == 1) {
                        changePath(&info);
                    }else {
                        printf("目录不存在\n");
                    }
                    printf("path=%s\n", info.path);
                    break;
                case 2:
                case 4:
                case 5:
                    //ls,mkdir,rm
                    sendInfoStruct(net_fd, &info);
                    recvInfoStruct(net_fd, &info);
                    printf("%s\n", info.command_str_return);
                    break;
                case 3:
                    //pwd
                    printf("current path: %s\n", info.path);
                    break;
                case 6:
                    //puts
                    sendInfoStruct(net_fd, &info);
                    
                    client_upload(net_fd, info);
                    break;
                case 7:
                    //puts
                    sendInfoStruct(net_fd, &info);
                    
                    client_download(net_fd);
                    break;
                /*case 4:
                    //mkdir
                    sendInfoStruct(net_fd, &info);
                    recvInfoStruct(net_fd, &info);
                    printf("%s\n", info.command_str_return);
                    break;
                case 5:
                    //rm
                    sendInfoStruct(net_fd, &info);
                    recvInfoStruct(net_fd, &info);
                    printf("%s\n", info.command_str_return);
                    
                    break;*/
                    /*case 1:
                    {
                        memset(&train, 0, sizeof(train));
                        train.len = strlen(info.command_str_goto);
                        send(net_fd, &train.len, sizeof(int), MSG_NOSIGNAL);
                        send(net_fd, info.command_str_goto, train.len, MSG_NOSIGNAL);
                        recv(net_fd, &flag_cd, sizeof(int), MSG_WAITALL);
                        //  memset(&train, 0, sizeof(train));
                        //  recv(net_fd,&train.len,sizeof(int),MSG_WAITALL);
                        //  memset(dir,0,sizeof(dir));
                        //  recv(net_fd,dir,train.len,MSG_WAITALL);
                        //  for(int i = 0;i<train.len;i++){
                        //      dir[i] = dir[i+1];
                        //  }
                        if(flag_cd == 1 ){
                            changeVirtualDir(&info, info.command_str_goto);    

                        }else{
                            printf("目录不存在\n");

                        }
                    }
                    break;
                case 2:
                    //ls
                    //memset(&train, 0, 1024);
                    //recv(net_fd, &train.len, sizeof(int), MSG_WAITALL);
                    //recv(net_fd, str, train.len, MSG_WAITALL);
                    //printf("%s\n",str);
                    break;
                case 3:
                    //pwd
                    break;
                case 4:

                    //mkdir
                    memset(&train, 0, sizeof(train));
                    train.len = strlen(info.command_str_goto);
                    send(net_fd, &train.len, sizeof(int), MSG_NOSIGNAL);
                    send(net_fd, info.command_str_goto, train.len, MSG_NOSIGNAL);

                    break;
                case 5:
                    //rm
                    memset(&train, 0, sizeof(train));
                    train.len = strlen(info.command_str_goto);
                    send(net_fd, &train.len, sizeof(int), MSG_NOSIGNAL);
                    send(net_fd, info.command_str_goto, train.len, MSG_NOSIGNAL);


                    break;
                case 6:
                    //puts
                    printf("filename=%s\n", info.command_str_goto);
                    client_upload(net_fd, "./", info.command_str_goto);
                    break;
                case 7:*/
                    //gets
                    /*memset(&train, 0, sizeof(train));
                      train.len = strlen(info.command_str_goto);
                      send(net_fd, &train.len, sizeof(int), MSG_NOSIGNAL);
                      send(net_fd, info.command_str_goto, train.len, MSG_NOSIGNAL);*/
                    /*sendCommand(net_fd, info.command_str_goto);
                    printf("%s\n", info.command_str_goto);
                    break;*/
                }
            }

            //监听服务端的返回
            if(events[i].data.fd == net_fd) {
                char buf[1024];
                ssize_t ret_recv = recv(net_fd, buf, sizeof(buf), 0);
                //服务端异常结束，客户端也结束
                if(ret_recv == 0) {
                    exit(1);
                }
               /* if(info.command_num == 7) {
                    //printf("da\n");
                    client_download(net_fd);
                }else if(info.command_num == 2) {
                    printf("in ls\n");
                    str = (char*)malloc(1024 * sizeof(char));
                    memset(str, 0, 1024);
                    int ret_recv = recv(net_fd, &train.len, sizeof(int), MSG_WAITALL);

                    ret_recv = recv(net_fd, str, train.len, MSG_WAITALL);
                    if(ret_recv == 0){break;}
                    printf("%s\n",str);
                }else if(info.command_num == 0) {
                }*/
            }
        }
    }

    close(net_fd);
    return 0;
}

